import request from '@/utils/request'
import { Message, Modal } from '@arco-design/web-vue'

const baseUrl = '/threshold-warn-back/threshold-warn-back/v1.0/'
const baseUrlAnalyse = '/constellation-correlate-analyse/constellation-correlate-analyse/v1.0/' // 常值监测  关联分析  时序计算
const baseUrlComplex = '/complex-model-back/complex-model-back/v1.0/' // 复杂模型
export const startOrStopBinding = (
  params = {
    bindingId: '', // 绑定id
    status: 0 // 状态 0:停止 1:启动
  }
) => {
  return request({
    url: baseUrl + 'binding/startOrStopBinding',
    method: 'get',
    params
  })
}
// 常值监测
const startOrStopBindingAnalyse = (
  params = {
    bindingId: '', // 绑定id
    status: 0 // 状态 0:停止 1:启动
  }
) => {
  return request({
    url: baseUrlAnalyse + 'constantParamBind/startOrStopBinding',
    method: 'get',
    params
  })
}

// 关联分析
const startOrStopBindingCorrelate = (
  params = {
    bindingId: '', // 绑定id
    status: 0 // 状态 0:停止 1:启动
  }
) => {
  return request({
    url: baseUrlAnalyse + 'expression/param/bind/startOrStopBinding',
    method: 'get',
    params
  })
}
// 时序计算
const startOrStopBindingTime = (
  params = {
    bindingId: '', // 绑定id
    status: 0 // 状态 0:停止 1:启动
  }
) => {
  return request({
    url: baseUrlAnalyse + 'timeSeries/param/bind/startOrStopBinding',
    method: 'get',
    params
  })
}
// 复杂模型
const startOrStopBindingComplex = (
  params = {
    bindingId: '', // 绑定id
    status: 0 // 状态 0:停止 1:启动
  }
) => {
  return request({
    url: baseUrlComplex + 'paramBind/startOrStopBinding',
    method: 'get',
    params
  })
}
export const handleRun = (record, enable = true, callback = () => {}) => {
  Modal.confirm({
    title: '提示',
    content: `确定要继续${enable ? '启用' : '停用'}吗？`,
    okText: '确定',
    cancelText: '取消',
    onOk: () => {
      const loading = Message.loading({
        content: '处理中...',
        duration: 0
      })

      startOrStopBinding({
        bindingId: record.id, // 绑定id
        status: enable ? 1 : 0
      })
        .then((res) => {
          if (res.code === 200) {
            Message.success('操作成功')
            callback()
          } else {
            Message.error(res.message)
          }
        })
        .catch((err) => {
          Message.error(err.message)
        })
        .finally(() => {
          loading.close()
        })
    }
  })
}
export const handleRunAnalyse = (record, enable = true, callback = () => {}) => {
  Modal.confirm({
    title: '提示',
    content: `确定要继续${enable ? '启用' : '停用'}吗？`,
    okText: '确定',
    cancelText: '取消',
    onOk: () => {
      const loading = Message.loading({
        content: '处理中...',
        duration: 0
      })

      startOrStopBindingAnalyse({
        bindingId: record.id, // 绑定id
        status: enable ? 1 : 0
      })
        .then((res) => {
          if (res.code === 200) {
            Message.success('操作成功')
            callback()
          } else {
            Message.error(res.message)
          }
        })
        .catch((err) => {
          Message.error(err.message)
        })
        .finally(() => {
          loading.close()
        })
    }
  })
}
export const handleRunCorrelate = (record, enable = true, callback = () => {}) => {
  Modal.confirm({
    title: '提示',
    content: `确定要继续${enable ? '启用' : '停用'}吗？`,
    okText: '确定',
    cancelText: '取消',
    onOk: () => {
      const loading = Message.loading({
        content: '处理中...',
        duration: 0
      })

      startOrStopBindingCorrelate({
        bindingId: record.id, // 绑定id
        status: enable ? 1 : 0
      })
        .then((res) => {
          if (res.code === 200) {
            Message.success('操作成功')
            callback()
          } else {
            Message.error(res.message)
          }
        })
        .catch((err) => {
          Message.error(err.message)
        })
        .finally(() => {
          loading.close()
        })
    }
  })
}
export const handleRunBindingTime = (record, enable = true, callback = () => {}) => {
  Modal.confirm({
    title: '提示',
    content: `确定要继续${enable ? '启用' : '停用'}吗？`,
    okText: '确定',
    cancelText: '取消',
    onOk: () => {
      const loading = Message.loading({
        content: '处理中...',
        duration: 0
      })

      startOrStopBindingTime({
        bindingId: record.id, // 绑定id
        status: enable ? 1 : 0
      })
        .then((res) => {
          if (res.code === 200) {
            Message.success('操作成功')
            callback()
          } else {
            Message.error(res.message)
          }
        })
        .catch((err) => {
          Message.error(err.message)
        })
        .finally(() => {
          loading.close()
        })
    }
  })
}
export const handleRunComplex = (record, enable = true, callback = () => {}) => {
  Modal.confirm({
    title: '提示',
    content: `确定要继续${enable ? '启用' : '停用'}吗？`,
    okText: '确定',
    cancelText: '取消',
    onOk: () => {
      const loading = Message.loading({
        content: '处理中...',
        duration: 0
      })

      startOrStopBindingComplex({
        bindingId: record.id, // 绑定id
        status: enable ? 1 : 0
      })
        .then((res) => {
          if (res.code === 200) {
            Message.success('操作成功')
            callback()
          } else {
            Message.error(res.message)
          }
        })
        .catch((err) => {
          Message.error(err.message)
        })
        .finally(() => {
          loading.close()
        })
    }
  })
}
